<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="广告位编号关联adv表" prop="adv_id">
                    <el-input v-model="formData.adv_id" clearable placeholder="请输入广告位编号关联adv表" />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="formData.sort" clearable placeholder="请输入排序" />
                </el-form-item>
                <el-form-item label="标题" prop="title">
                    <el-input v-model="formData.title" clearable placeholder="请输入标题" />
                </el-form-item>
                <el-form-item label="图片地址" prop="img">
                    <el-input v-model="formData.img" clearable placeholder="请输入图片地址" />
                </el-form-item>
                <el-form-item label="状态 1 启用 0 禁用" prop="status">
                    <el-input v-model="formData.status" clearable placeholder="请输入状态 1 启用 0 禁用" />
                </el-form-item>
                <el-form-item label="类型 0固定 1 指定列表 2商品详情 3 外部url" prop="type">
                    <el-input v-model="formData.type" clearable placeholder="请输入类型 0固定 1 指定列表 2商品详情 3 外部url" />
                </el-form-item>
                <el-form-item label="关联字段id" prop="param_id">
                    <el-input v-model="formData.param_id" clearable placeholder="请输入关联字段id" />
                </el-form-item>
                <el-form-item label="点击跳转的url地址" prop="url">
                    <el-input v-model="formData.url" clearable placeholder="请输入点击跳转的url地址" />
                </el-form-item>
                <el-form-item label="" prop="created_at">
                    <el-input v-model="formData.created_at" clearable placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="updated_at">
                    <el-input v-model="formData.updated_at" clearable placeholder="请输入" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { dictDataLists } from '@/api/setting/dict'
import { apiAdvInfoAdd, apiAdvInfoEdit } from '@/api/adv_info'
import { timeFormat } from '@/utils/util'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑广告列表' : '新增广告列表'
})

// 表单数据
const formData = reactive({
    id: '',
    adv_id: '',
    sort: '',
    title: '',
    img: '',
    status: '',
    type: '',
    param_id: '',
    url: '',
    created_at: '',
    updated_at: '',
})

// 字典数据
const dictData = reactive<Record<string, any[]>>({
    
})

// 获取字典数据
const getDictData = () => {

}

// 表单验证
const formRules = reactive<any>({
    adv_id: [{
        required: true,
        message: '请输入广告位编号关联adv表',
        trigger: ['blur']
    }],
    sort: [{
        required: true,
        message: '请输入排序',
        trigger: ['blur']
    }],
    title: [{
        required: true,
        message: '请输入标题',
        trigger: ['blur']
    }],
    img: [{
        required: true,
        message: '请输入图片地址',
        trigger: ['blur']
    }],
    status: [{
        required: true,
        message: '请输入状态 1 启用 0 禁用',
        trigger: ['blur']
    }],
    type: [{
        required: true,
        message: '请输入类型 0固定 1 指定列表 2商品详情 3 外部url',
        trigger: ['blur']
    }],
    created_at: [{
        required: true,
        message: '请输入',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    
    
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData,  }
    mode.value == 'edit' 
        ? await apiAdvInfoEdit(data) 
        : await apiAdvInfoAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

getDictData()

defineExpose({
    open,
    setFormData
})
</script>
